package md.lanjing.com.module_deal_center.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.google.gson.Gson;
import com.lxh.baselibray.constant.ARouterConfig;
import com.lxh.baselibray.dialog.AlertDialog;
import com.lxh.baselibray.mvp.MvpFragment;
import com.lxh.baselibray.util.ARouterUtils;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.SPUtils;
import com.lxh.baselibray.util.SizeUtils;
import com.lxh.baselibray.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import com.lxh.baselibray.constant.Constant;
import com.lxh.baselibray.event.EventImpl;


import md.lanjing.com.module_deal_center.R;
import md.lanjing.com.module_deal_center.R2;
import md.lanjing.com.module_deal_center.bean.DealCenterBuyListResponce;
import md.lanjing.com.module_deal_center.contact.DealCenterBuyContact;
import md.lanjing.com.module_deal_center.ui.activity.BuyDetailActivity;
import md.lanjing.com.module_deal_center.ui.adapter.BuyListAdapter;
import retrofit2.Response;

public class BuyFragment extends MvpFragment<DealCenterBuyContact.DealCenterBuyPresent> implements DealCenterBuyContact.IDealCenterBuyView {

    @BindView(R2.id.tv_type)
    TextView tvType;
    @BindView(R2.id.rv)
    RecyclerView rv;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;

    List<DealCenterBuyListResponce.DataBean> mList;
    BuyListAdapter mAdapter;
    private int pageNum = 1;
    private String type = "USDT";//全查

    @Override
    public void initData(Bundle savedInstanceState) {
        mList = new ArrayList<>();
        mAdapter = new BuyListAdapter(R.layout.recy_item_buy_list, mList);
        LinearLayoutManager manager = new LinearLayoutManager(context);
        rv.setLayoutManager(manager);
        rv.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            String realName = SPUtils.getString(Constant.IS_REAL, null, context);
            if (ObjectUtils.isEmpty(realName)) {
                showRemindDialog("你还未实名认证,去认证?");
                return;
            }
            Intent intent = new Intent(context, BuyDetailActivity.class);
            intent.putExtra(BuyDetailActivity.ORDER_BEAN, (Serializable) adapter.getData().get(position));
            startActivity(intent);
        });

        mPresent.getBuyList(context, type, pageNum);
        refresh.setOnRefreshListener(refreshLayout -> {
            pageNum = 1;
            mPresent.getBuyList(context, type, pageNum);
        });
        refresh.setOnLoadMoreListener(refreshLayout -> {
            pageNum++;
            mPresent.getBuyList(context, type, pageNum);
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_buy;
    }


    @OnClick(R2.id.tv_type)
    public void onViewClicked() {
        showTypeDialog();//功能隐藏，暂时不用
    }

    AlertDialog typeDialog = null;

    private void showTypeDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .addDefaultAnimation()
                .setCancelable(true)
                .setContentView(R.layout.dialog_type_select)
                .fromBottom(true)
                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, SizeUtils.dp2px(context, 80))
                .setOnClickListener(R.id.tv_one, v -> {
                    type="USDT";
                    tvType.setText(type);
                    pageNum=1;
                    mPresent.getBuyList(context, type, pageNum);
                    typeDialog.dismiss();
                }).setOnClickListener(R.id.tv_two, v -> {
                    type="CAED";
                    tvType.setText(type);
                    pageNum=1;
                    mPresent.getBuyList(context, type, pageNum);
                    typeDialog.dismiss();
                });
        typeDialog = builder.create();
        typeDialog.show();
    }

    @Override
    public void getBuyListData(Response<DealCenterBuyListResponce> response) {
        refresh.finishLoadMore();
        refresh.finishRefresh();
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            List<DealCenterBuyListResponce.DataBean> data = response.body().getData();
            if (pageNum == 1) {
                mList.clear();
            }
            mList.addAll(data);
            mAdapter.notifyDataSetChanged();
            if (data != null && data.size() < 10) {
                refresh.setEnableLoadMore(false);
            } else {
                refresh.setEnableLoadMore(true);
            }
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    protected DealCenterBuyContact.DealCenterBuyPresent createPresent() {
        return new DealCenterBuyContact.DealCenterBuyPresent();
    }


    @Override
    public boolean useEventBus() {
        return true;
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void consignReceive(EventImpl.ConsignEvent event){
        if (!ObjectUtils.isEmpty(event)){
            pageNum = 1;
            type="";
            mPresent.getBuyList(context, type, pageNum);
        }
    }

    AlertDialog remindDialog = null;
    private void showRemindDialog(String content) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .addDefaultAnimation()
                .setCancelable(true)
                .setContentView(R.layout.dialog_reimind)
                .setText(R.id.tv_content, content)
                .setWidthAndHeight(SizeUtils.dp2px(context, 279), SizeUtils.dp2px(context, 157))
                .setOnClickListener(R.id.tv_sure, v -> {
                    ARouter.getInstance().build(ARouterConfig.Mine_real_name_authen_act).navigation();
                    remindDialog.dismiss();
                }).setOnClickListener(R.id.tv_cancle, v -> remindDialog.dismiss());
        remindDialog = builder.create();
        remindDialog.show();
    }
}
